@extends('admin.common.main')
@section('css')
    {{--  引入webuploader css 异步文件上传  --}}
    <link rel="stylesheet" type="text/css" href="/webuploader/webuploader.css">
@endsection
@section('cnt')
    <nav class="breadcrumb">
        <i class="Hui-iconfont">&#xe67f;</i> 首页
        <span class="c-gray en">&gt;</span> 房源管理
        <span class="c-gray en">&gt;</span> 房东添加
        <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="Hui-iconfont">&#xe68f;</i>
        </a>
    </nav>

    <article class="page-container">

        @include('admin.common.msg')
        @include('admin.common.validate')

        <form action="{{ route('admin.fangowner.store') }}" class="form form-horizontal" @submit.prevent="dopost"
              id="form-member-add">

            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>房东姓名：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="name" v-model.lazy="info.name">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>性别：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="radio" name="gender" value="男" checked @click="get_gender('男')">男士
                    <input type="radio" name="gender" value="女" @click="get_gender('女')">女士
                </div>
            </div>

            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>年龄：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="age" v-model.lazy="info.age">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>手机号：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="phone" v-model.lazy="info.phone">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>地址：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="address" v-model.lazy="info.address">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>身份证号：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="card" v-model.lazy="info.card">
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">身份证照片：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="hidden" class="input-text" name="pic" v-model.lazy="info.pic">
                    <input type="hidden" class="input-text" name="pic1" v-model.lazy="info.pic1">
                    <input type="hidden" class="input-text" name="pic2" v-model.lazy="info.pic2">
                    <div id="fileList" class="uploader-list">
                        <img :src="info.pic" id="img" style="width: 160px;height: 100px">
                        <strong class="delimg" @click="delimg(1)">X</strong>
                        <img :src="info.pic1" id="img" style="width: 160px;height: 100px">
                        <strong class="delimg" @click="delimg(2)">X</strong>
                        <img :src="info.pic2" id="img" style="width: 160px;height: 100px">
                        <strong class="delimg" @click="delimg(3)">X</strong>
                    </div>
                    <br>
                    <span class="filePicker" @click="add_id(1)">正面</span>
                    <span class="filePicker" @click="add_id(2)">反面</span>
                    <span class="filePicker" @click="add_id(3)">手持</span>
                </div>
            </div>
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">
                    <span class="c-red">*</span>邮箱：
                </label>
                <div class="formControls col-xs-8 col-sm-9">
                    <input type="text" class="input-text" name="email" v-model.lazy="info.email">
                </div>
            </div>

            <div class="row cl">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                    <input class="btn btn-primary radius" type="submit" value="添加节点">
                </div>
            </div>
        </form>
    </article>


    <style>
        .filePicker {
            margin-right: 60px;
            margin-left: 50px;
        }

        .delimg {
            cursor: pointer;
        }
    </style>
@endsection

@section('js')
    <script src="/js/vue.js"></script>
    {{-- webuploader js 异步文件上传 --}}
    <script type="text/javascript" src="/webuploader/webuploader.js"></script>
    {{--// 引入验证的jquery--}}
    <script>
        new Vue({
            el: '.page-container',
            // 数据
            data: {
                info: {
                    _token: '{{ csrf_token() }}',
                    name: '',
                    gender: '男',
                    age: '',
                    phone: '',
                    address: '',
                    card: '',
                    email: '',
                    img_id: 0,
                    pic: '{{ config('uploadpic.img') }}',
                    pic1: '{{ config('uploadpic.img') }}',
                    pic2: '{{ config('uploadpic.img') }}',
                }
            },
            methods: {
                async dopost(evt) {
                    let url = evt.target.action;
                    let data = this.info;
                    let {msg, status} = await $.post(url, data);
                    if (status == 200) {
                        layer.msg(msg, {icon: 1, time: 900});
                        location.href = '{{ route('admin.fangowner.index') }}';
                    } else {
                        layer.msg(msg, {icon: 2, time: 900});
                    }
                },
                // 设置标签
                add_id($id) {
                    this.info.img_id = $id;
                },
                // 性别获取
                get_gender($gender) {
                    this.info.gender = $gender;
                },
                // 删除图片
                delimg($id) {
                    if ($id == 1) {
                        this.info.pic = '{{ config('uploadpic.img') }}';
                    } else if ($id == 2) {
                        this.info.pic1 = '{{ config('uploadpic.img') }}';
                    } else if ($id == 3) {
                        this.info.pic2 = '{{ config('uploadpic.img') }}';
                    } else {
                        this.info.pic = '{{ config('uploadpic.img') }}';
                    }
                }
            },
            // 生命周期
            mounted() {
                // 初始化Web Uploader
                this.uploader = WebUploader.create({
                    // 选完文件后，是否自动上传。
                    auto: true,
                    // swf文件路径
                    swf: '/js/Uploader.swf',
                    // post上传的携带的数据
                    formData: {
                        _token: "{{ csrf_token() }}"
                    },
                    // 文件接收服务端。
                    server: '{{ route('admin.fangowner.upimg') }}',
                    // 内部根据当前运行是创建，可能是input元素，也可能是flash.
                    pick: {
                        id: '.filePicker',
                        // 是否允许同时上传多个文件
                        multiple: false,
                    },
                    // 只允许选择图片文件。
                    accept: {
                        title: 'Images',
                        extensions: 'gif,jpg,jpeg,bmp,png',
                        mimeTypes: 'image/*'
                    },
                });
                // 图片上传成功
                this.uploader.on('uploadSuccess', (file, ret) => {
                    // console.log(ret.src);
                    if (this.info.img_id == 1) {
                        this.info.pic = ret.src;
                    } else if (this.info.img_id == 2) {
                        this.info.pic1 = ret.src;
                    } else if (this.info.img_id == 3) {
                        this.info.pic2 = ret.src;
                    } else {
                        this.info.pic = ret.src;
                    }
                });
            }
        })
    </script>
@endsection
